/*
 * Author 	: Anas Nakawa
 * Date		: 23-01-2011
 * We create custom selectors here, to easily differenciate each parameter according
 * to our specifications..
 * e.g  : 
 * note : commons.xxx will be loaded from json.commons.js file
 */

$.expr[':'].someSelector = function(obj) {
	return ($(obj).attr(commons.attributes.someType) == '');
}

$.expr[':'].someRange = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
	return isInBetween(objNode, arrProperties, commons.attributes.age);
}

// select by record category 
$.expr[':'].category = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
	return hasCategory(objNode, arrProperties, commons.attributes.category);
}

$.expr[':'].amountRange = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
	return isInBetween(objNode, arrProperties, commons.attributes.amount);
}

// select all data between two dates by passing time in milliseconds
$.expr[':'].dateRange = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
	return isDateInBetween(objNode, arrProperties, commons.attributes.date);
}

function hasCategory(objNode, arrProperties, attribute) {
	var arrArgument = arrProperties[ 3 ];
	var category = $(objNode).attr(attribute);
	return (category == arrArgument);
}

function isDateInBetween(objNode, arrProperties, attribute) {
	// parameters will be passed by default at 3rd element in arrProperties array
	var arrArguments = eval(
	"([" + arrProperties[ 3 ] + "])"
	);
	var firstValue = arrArguments[0];
	var secondValue = arrArguments[1];
	var value = parseInt($(objNode).attr(attribute));
	if(arrArguments.length == 2 && !isNaN(value)) {
		var minValue = (firstValue == -1 ? Number.NEGATIVE_INFINITY : firstValue);
		var maxValue = (secondValue == -1 ? Number.POSITIVE_INFINITY : secondValue);
		return ( minValue <= value && value <= maxValue );
	}
	return false;
}

function isInBetween(objNode, arrProperties, attribute) {
	// parameters will be passed by default at 3rd element in arrProperties array
	var arrArguments = eval(
	"([" + arrProperties[ 3 ] + "])"
	);
	var value = parseInt($(objNode).attr(attribute));
	if(arrArguments.length == 2 && !isNaN(value)) {
		var minValue = (arrArguments[0] == -1 ? Number.NEGATIVE_INFINITY : arrArguments[0]);
		var maxValue = (arrArguments[1] == -1 ? Number.POSITIVE_INFINITY : arrArguments[1]);
		return ( minValue <= value && value <= maxValue );
	}
	return false;
}
